<?php
final class Customer {
	private $customer_id;
	private $firstname;
	private $lastname;
	private $email;
	private $telephone;
	private $fax;
	private $newsletter;
	private $customer_group_id;
	private $address_id;
	
  	public function __construct() {
		$CI =& get_instance();
				
		if ($CI->session->userdata('customer_id')) { 
			$customer_query = $CI->db->query("SELECT * FROM " . $CI->db->dbprefix . "customer WHERE customer_id = " . (int)$CI->session->userdata('customer_id') . " AND status = '1'");
			
			if ($customer_query->num_rows()) {
				$customer_row = $customer_query->row_array();
				$this->customer_id = $customer_row['customer_id'];
				$this->firstname = $customer_row['firstname'];
				$this->lastname = $customer_row['lastname'];
				$this->email = $customer_row['email'];
				$this->telephone = $customer_row['telephone'];
				$this->fax = $customer_row['fax'];
				$this->newsletter = $customer_row['newsletter'];
				$this->customer_group_id = $customer_row['customer_group_id'];
				$this->address_id = $customer_row['address_id'];
							
      			$CI->db->query("UPDATE " . $CI->db->dbprefix . "customer SET cart = " . $CI->db->escape($CI->session->userdata('cart') ? serialize($CI->session->userdata('cart')) : '') . ", wishlist = " . $CI->db->escape($CI->session->userdata('wishlist') ? serialize($CI->session->userdata('wishlist')) : '') . ", ip = " . $CI->db->escape($CI->input->server('REMOTE_ADDR')) . " WHERE customer_id = " . (int)$CI->session->userdata['customer_id'] . "");
			
				$query = $CI->db->query("SELECT * FROM " . $CI->db->dbprefix . "customer_ip WHERE customer_id = " . (int)$CI->session->userdata('customer_id') . " AND ip = " . $CI->db->escape($CI->input->server('REMOTE_ADDR')) . "");
				
				if (!$query->num_rows()) {
					$CI->db->query("INSERT INTO " . $CI->db->dbprefix . "customer_ip SET customer_id = " . (int)$CI->session->userdata('customer_id') . ", ip = " . $CI->db->escape($CI->input->server('REMOTE_ADDR')) . ", date_added = NOW()");
				}
			} else {
				$this->logout();
			}
  		}
	}
		
  	public function login($email, $password) {
		$CI =& get_instance();
		if (!$CI->cconfig->get('config_customer_approval')) {
			$customer_query = $CI->db->query("SELECT * FROM " . $CI->db->dbprefix . "customer WHERE LOWER(email) = " . $CI->db->escape(strtolower($email)) . " AND password = " . $CI->db->escape(md5($password)) . " AND status = '1'");
		} else {
			$customer_query = $CI->db->query("SELECT * FROM " . $CI->db->dbprefix . "customer WHERE LOWER(email) = " . $CI->db->escape(strtolower($email)) . " AND password = " . $CI->db->escape(md5($password)) . " AND status = '1' AND approved = '1'");
		}
		
		if ($customer_query->num_rows()) {
			$customer_row = $customer_query->row_array();
			$CI->session->set_userdata('customer_id', $customer_row['customer_id']);	
		    
			if (($customer_row['cart']) && (is_string($customer_row['cart']))) {
				$cart = unserialize($customer_row['cart']);
				
				foreach ($cart as $key => $value) {
					$session_cart = $CI->session->userdata('cart');
					if (!array_key_exists($key, $session_cart)) {
						$session_cart[$key] = $value;
					} else {
						$session_cart[$key] += $value;
					}
					$CI->session->set_userdata('cart', $session_cart);
				}			
			}

			if (($customer_row['wishlist']) && (is_string($customer_row['wishlist']))) {
				if (!$CI->session->userdata('wishlist')) {
					$CI->session->set_userdata('wishlist', array());
				}
								
				$wishlist = unserialize($customer_row['wishlist']);
			
				$session_wishlist = $CI->session->userdata('wishlist');
				foreach ($wishlist as $product_id) {
					if (!in_array($product_id, $session_wishlist)) {
						$session_wishlist[] = $product_id;
					}
				}			
				$CI->session->set_userdata('wishlist', $session_wishlist);	
			}
									
			$this->customer_id = $customer_row['customer_id'];
			$this->firstname = $customer_row['firstname'];
			$this->lastname = $customer_row['lastname'];
			$this->email = $customer_row['email'];
			$this->telephone = $customer_row['telephone'];
			$this->fax = $customer_row['fax'];
			$this->newsletter = $customer_row['newsletter'];
			$this->customer_group_id = $customer_row['customer_group_id'];
			$this->address_id = $customer_row['address_id'];
          	
			$CI->db->query("UPDATE " . $CI->db->dbprefix . "customer SET ip = " . $CI->db->escape($CI->input->server('REMOTE_ADDR')) . " WHERE customer_id = " . (int)$customer_row['customer_id'] . "");
			
	  		return true;
    	} else {
      		return false;
    	}
  	}
  
  	public function logout() {
		$CI =& get_instance();
		$CI->session->unset_userdata('customer_id');

		$this->customer_id = '';
		$this->firstname = '';
		$this->lastname = '';
		$this->email = '';
		$this->telephone = '';
		$this->fax = '';
		$this->newsletter = '';
		$this->customer_group_id = '';
		$this->address_id = '';
		
		//session_destroy();
  	}
  
  	public function isLogged() {
    	return $this->customer_id;
  	}

  	public function getId() {
    	return $this->customer_id;
  	}
      
  	public function getFirstName() {
		return $this->firstname;
  	}
  
  	public function getLastName() {
		return $this->lastname;
  	}
  
  	public function getEmail() {
		return $this->email;
  	}
  
  	public function getTelephone() {
		return $this->telephone;
  	}
  
  	public function getFax() {
		return $this->fax;
  	}
	
  	public function getNewsletter() {
		return $this->newsletter;	
  	}

  	public function getCustomerGroupId() {
		return $this->customer_group_id;	
  	}
	
  	public function getAddressId() {
		return $this->address_id;	
  	}
	
  	public function getBalance() {
		$CI =& get_instance();
		$query = $CI->db->query("SELECT SUM(amount) AS total FROM " . $CI->db->dbprefix . "customer_transaction WHERE customer_id = '" . (int)$this->customer_id . "'");
	
		return $query->row()->total;
  	}	
		
  	public function getRewardPoints() {
		$CI =& get_instance();
		$query = $CI->db->query("SELECT SUM(points) AS total FROM " . $CI->db->dbprefix . "customer_reward WHERE customer_id = '" . (int)$this->customer_id . "'");
	
		return $query->row()->total;
  	}	
}
?>